import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

class Book{
    public String name;
    public String number;

    public Book(String name, String number) {
        this.name = name;
        this.number = number;
    }
}
@WebServlet("/newBook")
public class NewBook extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=utf-8");
        List<Book> list = new ArrayList<>();
        list.add(new Book("美国必死","确定"));
        list.add(new Book("美国必死","确定"));
        list.add(new Book("美国必死","确定"));
        WebContext webContext = new WebContext(req,resp,this.getServletContext());
        webContext.setVariable("persons",list);
        TemplateEngine engine = (TemplateEngine) this.getServletContext().getAttribute("engine");
        String html = engine.process("addressBook",webContext);
        System.out.println(html);
        resp.getWriter().write(html);
    }
}
